package pl.enigmatic.parser.xml;

import pl.enigmatic.util.Arrays;
import pl.enigmatic.util.Reflection;
import pl.enigmatic.parser.string.ArrayConverter;
import pl.enigmatic.parser.string.Converter;


@SuppressWarnings("rawtypes")
class PrimitiveArrayParser extends Parser {

	private final Class<?> base;
	private final int depth;
	private final Parser<?> parser;

	@SuppressWarnings("unchecked")
	protected PrimitiveArrayParser(final Class<?> arraycls) {
		super(arraycls, false);
		base = Reflection.arrayBase(arraycls);
		depth = Reflection.arrayDepth(arraycls);
		parser = createParser(Reflection.arrayType(Reflection.wrapper(base), depth));
	}

	@Override
	protected Converter<?> createConverter() {
		if (Reflection.arrayDepth(getClazz()) > ArrayConverter.MAX_DEPTH) {
			return null;
		} else {
			return super.createConverter();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object parse(final ParsingInfo info) {
		return Arrays.convert(parser.parse(info));
	}

	/**
	 * @return the base
	 */
	public Class<?> getBase() {
		return base;
	}

	/**
	 * @return the depth
	 */
	public int getDepth() {
		return depth;
	}

}
